/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utilities;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import org.imgscalr.Scalr;

/**
 *
 * @author cristianbertuccioli
 */
public class AvatarGenerator {
    
    private String avatarPath;

    public AvatarGenerator(String path, ArrayList<BufferedImage> images, int extensionNumber) {


        try {
            File f = new File(path);
            File[] files = f.listFiles(new FilenameFilter() {
                @Override
                public boolean accept(File dir, String name) {
                    return name.startsWith("avatar-");
                }
            });
            for(File currentFile : files)
                currentFile.delete();
            
            
            if (images == null) {
                BufferedImage im1 = ImageIO.read(new File(path + "mumuverdi.png"));
                BufferedImage im2 = ImageIO.read(new File(path + "i7229.jpg"));
                BufferedImage im3 = ImageIO.read(new File(path + "sushi.jpg"));
                BufferedImage im4 = ImageIO.read(new File(path + "spiaggia.jpg"));
                BufferedImage im5 = ImageIO.read(new File(path + "piciolone.png"));
                
                
                images = new ArrayList<BufferedImage>();
                System.out.println("popolato debug mode!!!!");
                images.add(im1);
                images.add(im2);
                images.add(im3);
                images.add(im4);
                images.add(im5);
            }
            
            int step = Math.round(360/images.size()); // determina di quanto larghi devono essere gli spicchi
//            step = 45;
            double stepRad = Math.toRadians(step);
            System.out.println("Step deg: "+step+", rad: " + stepRad);
            
            ArrayList<BufferedImage> generatedImgs = new ArrayList<BufferedImage>();
            
            int i = 0;
            BufferedImage bi;
            Iterator<BufferedImage> iterator = images.iterator();
            while (iterator.hasNext()) {
                BufferedImage currentImg = iterator.next();
                
                
                currentImg = Scalr.resize(currentImg, Scalr.Mode.FIT_TO_HEIGHT, 200, 200);
                currentImg = Scalr.crop(currentImg, 200, 200);
                
                bi = new BufferedImage(currentImg.getWidth(null), currentImg.getHeight(null), BufferedImage.TYPE_INT_ARGB);
                Graphics2D g2 = (Graphics2D) bi.getGraphics();
                g2.setComposite(AlphaComposite.Src);
                // .. set attributes ..
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
                g2.setRenderingHint(RenderingHints.KEY_RENDERING,
                        RenderingHints.VALUE_RENDER_QUALITY);
                g2.setColor(Color.WHITE);
                
//                
                
                double origX = bi.getWidth() / 2; // punto centrale
                double origY = bi.getHeight() / 2;
                
                int angle = step * i;
                double angleRad = Math.toRadians(angle);
                System.out.println("Angolo deg: "+angle+", rad: "+angleRad);
                
                double firstX = origX + 200 * Math.sin(angleRad);
                double firstY = origY + 200 * Math.cos(angleRad);
                System.out.println("FirstX: "+firstX+", FirstY: "+firstY);
                
                double secondX = origX + 200 * Math.sin(angleRad + stepRad);
                double secondY = origY + 200 * Math.cos(angleRad + stepRad);
                System.out.println("SecondX: "+secondX+", SecondY: "+secondY);

                GeneralPath p = new GeneralPath();
                p.moveTo(origX, origY); // centro dell'immagine
                p.lineTo(firstX, firstY); // traccio la linea superiore
                p.lineTo(secondX, secondY); // traccio la linea inferiore
                p.closePath();
                g2.fill(p);
                
//                g2.clip(p);
//                Ellipse2D ellipse = new Ellipse2D.Float();
//                ellipse.setFrame(0, 0, bi.getWidth(), bi.getHeight());
//                g2.setClip(ellipse);
                
                g2.setComposite(AlphaComposite.SrcAtop);
                
                g2.drawImage(currentImg, 0, 0, null);
                g2.dispose();
                
                generatedImgs.add(bi);
                
                
//                ImageIO.write(bi, "png", new File(path + i +".png"));
                i++;
            }
            
            
            Iterator<BufferedImage> bufferedIt = generatedImgs.iterator();
            BufferedImage combined = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g = (Graphics2D) combined.getGraphics();
//            g.setComposite(AlphaComposite.Src);
            g.setComposite(AlphaComposite.Src);
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            g.setColor(Color.WHITE);
            g.fillOval(0, 0, combined.getWidth(), combined.getHeight());
            
            g.setComposite(AlphaComposite.SrcAtop);
            while (bufferedIt.hasNext()) {
                BufferedImage currentBI = bufferedIt.next();
                System.out.println(currentBI.getWidth());
                g.drawImage(currentBI, 0, 0, null);
            }
            g.dispose();
            ImageIO.write(combined, "png", new File(path +"avatar-" + extensionNumber +".png"));
            
            // copy to Avatar folder
            
            
            avatarPath = path +"avatar-" + i +".png";

        } catch (IOException ex) {
            Logger.getLogger(AvatarGenerator.class.getName()).log(Level.SEVERE, null, ex);
        }




    }
    
    public String getAvatarPath() {
        return avatarPath;
    }
}
